#include "task_indicator_led_and_input.h"

#include "FreeRTOS.h"
#include "task.h"

#include "input_func.h"
#include "led_func.h"
#include "print_rtt.h"
#include "system_led.h"
#include "iwdg_func.h"
#include "rs485_comm_analyse_common.h"
#include "print_rtt.h"
// #include "OutputLog.h"
static StackType_t xIndicatorLedAndInputStack[128]; // 静态栈
static StaticTask_t xIndicatorLedAndInputTCB;       // 静态 TCB（可选，省堆）
static void vIndicatorLedAndInputTaskEntry(void *pvParameters)
{
  (void)pvParameters;

  for (;;)
  {
    input_status_check_cycle();
    led_blink_cycle();
    // system_led_toggle();//第二个灯
    // system_net_status_toggle(); // 中间的那个灯
    // system_warning_toggle();//最高的红灯
    if(ctrl_several==1)
    {
      int i=0;
      CTRL_REQ_ORDER_STRUCT cro_st;
      for(i=0;i<12;i++)
      {
              
//      LOG_INFO("line no.%d,status: %d\n", line_no->valueint, status->valueint);
      cro_st.ctrl_unit_index = i;
      cro_st.order_type = CONTROL_CMD;
      // if (status->valueint == 1)
      // {
        cro_st.order_data = 0x00AA;
      // }
      // else if (status->valueint == 0)
      // {
      //   cro_st.order_data = 0x0033;
      // }
      // else
      // {
      //   LOG_ERROR("no.%d 's line no.and status is invalid\n", i + 1);
      //   continue;
      // }
      if (xQueueSend(xCtrlUnitQueueHandle, &cro_st.ctrl_unit_index, 0U) == pdPASS)
      {
        LOG_INFO("send data success!\r\n");
      }
      }
      ctrl_several=0;
    }
    IWDG_Feed();
    vTaskDelay(pdMS_TO_TICKS(50));
  }
}

void vIndicatorLedAndInputTaskCreate(void)
{
  TaskHandle_t xHandle = NULL;
  taskENTER_CRITICAL(); // 进入临界区

  system_led_init();
  system_net_status_init();
  system_warning_init();
  register_led_interface(SYSTEM_LED, FLASH_LONG_AVERAGE, system_led_on, system_led_off);
  register_led_interface(NET_STATUS_LED, LED_OFF, net_status_led_on, net_status_led_off);
  register_led_interface(WARNING_LED, FLASH_SHORT_AVERAGE, warning_led_on, warning_led_off);
  LightningStrikeFeedbackSignalHardInit();
  ManualControlSignalHardInit();
  /* 创建system_led任务 - 静态方法 */
  xHandle = xTaskCreateStatic(
      (TaskFunction_t)vIndicatorLedAndInputTaskEntry,                     /* 任务入口函数 */
      (const char *)"led_&_input",                                        /* 任务名字 */
      (uint16_t)sizeof(xIndicatorLedAndInputStack) / sizeof(StackType_t), /* 任务栈大小 */
      (void *)NULL,                                                       /* 任务入口函数参数 */
      (UBaseType_t)5,                                                     /* 任务的优先级 */
      (StackType_t *)xIndicatorLedAndInputStack,                          /* 静态栈数组 */
      (StaticTask_t *)&xIndicatorLedAndInputTCB);                         /* 静态TCB的地址 */

  if (xHandle != NULL)
  {
    LOG_INFO("Create indicator led and input task success!\r\n");
  }
  taskEXIT_CRITICAL(); // 退出临界区，无论成功失败都要退出
}
